package com.cartrentalsystem.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.captcha.generator.RandomGenerator;
import com.cartrentalsystem.domain.po.User;
import com.cartrentalsystem.domain.vo.EmployeeVo;
import com.cartrentalsystem.domain.vo.Result;
import com.cartrentalsystem.domain.vo.UserVo;
import com.cartrentalsystem.service.ICustomersService;
import com.cartrentalsystem.service.IEmployeeService;
import com.cartrentalsystem.service.IUserService;
import com.cartrentalsystem.utils.BcryptUtils;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/login")
@Slf4j
@RequiredArgsConstructor
public class LoginController {

    private final IUserService userService;

    private final RedisTemplate redisTemplate;

    private final IEmployeeService employeeService;

    @PostMapping("/admin")
    public Result<UserVo> loginAdmin(@RequestBody UserVo userVo, HttpServletResponse  response){
        log.info("管理员登录：{},{},{},{}", userVo.getUsername(), userVo.getPassword(),userVo.getUUID(), userVo.getCode());
        UserVo user1 = userService.login(userVo.getUsername(), userVo.getPassword(), userVo.getUUID(), userVo.getCode(),response);
        return Result.success(user1);
    }

    @PostMapping("/employee")
    public Result<EmployeeVo> loginEmployee(@RequestBody EmployeeVo userVo, HttpServletResponse  response){
        log.info("员工登录：{},{},{},{}", userVo.getUsername(), userVo.getPassword(),userVo.getUUID(), userVo.getCode());
        EmployeeVo user1 = employeeService.login(userVo.getUsername(), userVo.getPassword(), userVo.getUUID(), userVo.getCode(),response);
        return Result.success(user1);
    }

    @PostMapping("/register")
    public Result<String> register(@RequestBody User user){
        log.info("用户注册：{},{}", user.getUsername(), user.getPassword());
        log.info("用户信息：{}", user);
        String encrypt = BcryptUtils.encrypt(user.getPassword());
        user.setPassword(encrypt);
        boolean register = userService.save(user);
        if (register){
            return Result.success("注册成功");
        }
        return Result.fail("注册失败");
    }

    @GetMapping("/captcha")
    public void createCaptcha(HttpServletResponse response) throws IOException {
        log.info("Generating captcha...");
        // 定义图形验证码的长和宽
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100);
        // 图形验证码写出，可以写出到文件，也可以写出到流
        lineCaptcha.write(response.getOutputStream());
        String code = lineCaptcha.getCode();
        UUID uuid = UUID.randomUUID();
        redisTemplate.opsForValue().set("Captcha"+uuid,code, 60, TimeUnit.SECONDS);
        response.setHeader("UUID", String.valueOf(uuid));
        // 关闭流
        response.getOutputStream().close();
    }


}
